package com.lcy.mybatis.generator.plugin.element;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

/**
 * 根据某一列进行选择：
 * select * from table where column = value
 *
 * <p>创建时间：2017/5/4 16:57</p>
 *
 * @author 娄存银
 * @version 1.0
 */
public class DeleteByColumnElement extends CustomerSqlElement{
    private static final String SQL = "delete from %s where \n" +
            "        <if test=\"column != null and value != null\">\n" +
            "            ${column} = #{value}\n" +
            "        </if>\n" +
            "        <if test=\"column != null and value == null\">\n" +
            "            ${column} is null\n" +
            "        </if>\n" +
            "        <if test=\"column == null\">\n" +
            "            false\n" +
            "        </if>";
    public DeleteByColumnElement(IntrospectedTable table) {
        super(table);
    }

    @Override
    public void initXmlElement() {
        xmlElement = deleteElement();
    }

    @Override
    protected void initMethod() {
        method = new Method(getId());

        method.setReturnType(TYPE_INTEGER);

        method.addParameter(new Parameter(TYPE_STRING,COLUMN,annotation(COLUMN)));
        method.addParameter(new Parameter(TYPE_OBJECT,VALUE,annotation(VALUE)));

    }

    @Override
    protected String getId() {
        return "deleteByColumn";
    }

    @Override
    protected String getSql() {
        return SQL;
    }
}
